package com.sg.web;

import com.sg.bean.Jobperson;
import com.sg.service.JobpersonService;
import com.sg.vo.JobpersonVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * @author lyx
 * 工作发布人控制器
 */
@Controller
//@RequestMapping("/join")
public class JobPersonController {
    Logger logger = Logger.getLogger(JobPersonController.class);
    @Autowired
    JobpersonService jobpersonService;

    @RequestMapping("/join/toregistjoin")
    public String toregistjoin() {
        logger.info("toregistjoin---");
        return "/join/registjoin";
    }

    @RequestMapping("/join/registjoin")
    public String registjoin(Jobperson jobperson, Model model) {
        logger.info("registjoin---");
        logger.info(jobperson);
        int jpId = jobpersonService.add(jobperson);
        return "/join/loginjoin";
    }

    @RequestMapping("/join/tologinjoin")
    public String tologinjoin() {
        logger.info("tologinjoin---");
        return "/join/loginjoin";
    }

    @RequestMapping("/join/loginjoin")
    public String loginjoin(HttpSession session, Jobperson jobperson, Model model) {
        logger.info(jobperson);
        JobpersonVO jobpersonVO = jobpersonService.findByPassAndName(jobperson);
        if (jobpersonVO != null) {
            logger.info(jobpersonVO.getJpName());
            session.setAttribute("currentJobperson", jobpersonVO);
            return "redirect:mainjoin.action";
        } else {
            //用户名或密码错误
            model.addAttribute("mess", "用户名或密码错误");
            return "/join/loginjoin";
        }
    }

    @RequestMapping("/admin/tojoblogin")
    public String tojoblogin() {
        logger.info("tojoblogin---");
        return "/admin/joblogin";
    }

    @RequestMapping("/admin/joblogin")
    public String adminloginjoin(HttpSession session, Jobperson jobperson, Model model) {
        logger.info(jobperson);
        JobpersonVO jobpersonVO = jobpersonService.findByPassAndName(jobperson);
        logger.info(jobpersonVO.getJpName());
        if (jobpersonVO != null) {
//            jobpersonVO.getJpName() == "管理员"&&jobpersonVO.getJpPass() == "666"

            session.setAttribute("currentJobperson", jobpersonVO);
            return "redirect:jobmain.action";
        } else {
            //用户名或密码错误
            model.addAttribute("mess", "用户名或密码错误");
            return "/admin/joblogin";
        }
    }

    @RequestMapping("/join/exitjoin")
    public String exitjoin(HttpSession session){
        logger.info("exitjoin---");
        session.invalidate();//让当前会话失效
        return "/join/loginjoin";
    }

    @RequestMapping("/admin/jobexit")
    public String jobexit(HttpSession session){
        logger.info("jobexit---");
        session.invalidate();//让当前会话失效
        return "/admin/joblogin";
    }
}
